package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.OrderBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.view.ExchangeOrderContract;
import com.mz.ozy.mvp.view.PrivateOrderContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/12/7.
 */

public class PrivateOrderPresenter extends BasePresenterImpl<PrivateOrderContract.View> implements PrivateOrderContract.Presenter {

    @Override
    public void detailSubmit(Activity activity, String id, String gid, String aid, String freightId, String remark, String cardid) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", id);
        maps.put("attribute_id", gid);
        maps.put("addressId", aid);
        maps.put("freightId", freightId);
        if (!TextUtils.isEmpty(remark)) {
            maps.put("remark", remark);
        }
        if (!TextUtils.isEmpty(cardid)) {
            maps.put("cardid", cardid);
        }
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10090, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OrderBean>(mView, true) {

                    @Override
                    public void onSuccesss(OrderBean orderBean) {
                        mView.onSubmitSuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onSubmitError("");
                    }
                });
    }

    @Override
    public void onPay(Context contex, String orderId, String type, String pwd) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", orderId);
        maps.put("payType", type);
        maps.put("payPwd", MD5Util.getMD5String(pwd));
        RetrofitUtils.getApiService(contex).post(Constants.TransCode.TransCode10091, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView, true) {

                    @Override
                    public void onSuccesss(JSONObject orderBean) {
                        mView.onPaySuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onPayError("");
                    }
                }.setTransCode(Constants.TransCode.TransCode10091));
    }
}
